﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pLib
{
    enum FILEIO
    {
        /// <summary>
        /// File IO Successfully
        /// </summary>
        SUCESS,
        /// <summary>
        /// File IO Fail
        /// </summary>
        FAIL,
    };
    public class MyFile
    {
        /// <summary>
        /// Save string into files
        /// </summary>
        /// <param name="saveString"></param>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        FILEIO SaveStringToFile(string saveString, string fullPath)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(fullPath))
                {
                    sw.Write(saveString);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception)
            {
                return FILEIO.FAIL;
            }

            return FILEIO.SUCESS;
        }
        FILEIO DeleteFolderAndSubChild(string fullPath)
        {
            DirectoryInfo di = new DirectoryInfo(fullPath);
            if (di != null)
            {
                try
                {
                    di.Delete(true);
                    return FILEIO.SUCESS;
                }
                catch (Exception)
                {
                    return FILEIO.FAIL;
                }
            }
            return FILEIO.FAIL;
        }

        //basepath = @"C:\awork\Development\Pacom"
        public void GetAllDirList(string basepath)
        {
            DirectoryInfo di = new DirectoryInfo(basepath);

            DirectoryInfo[] dis = di.GetDirectories();

            for (int i = 0; i < dis.Length; i++)
            {
                /*
                 * 
                 */

                if (dis[i].Name == "ThirdParty")
                    continue;

                if (dis[i].Name == "bin")
                {
                    Console.WriteLine(dis[i].FullName);
                    dis[i].Delete(true);
                    continue;
                }

                GetAllDirList(dis[i].FullName);
            }
        }

        /// <summary>
        /// (Copy-File Src Des)
        /// </summary>
        /// <param name="pathFrom"></param>
        /// <param name="pathTo"></param>
        /// <returns></returns>
        public int CopyFile(string pathSource, string pathNew)
        {
            if (File.Exists(pathSource) == false)
            {
                return 1;
            }

            //Regex r = new Regex("^([a-zA-Z]:)?(\\[^<>:\"/\\|?*]+)+\\?$");
            //Match m = r.Match(pathNew);

            const int BufferLength = 100;
            try
            {

                using (FileStream fsSource = new FileStream(pathSource,
                    FileMode.Open, FileAccess.Read))
                {
                    // Read the source file into a byte array.
                    byte[] bytes = new byte[BufferLength];
                    long numBytesToRead = fsSource.Length;

                    long offset = 0;

                    FileStream fsNew = new FileStream(pathNew,
                        FileMode.Create, FileAccess.Write);

                    while (offset < fsSource.Length)
                    {
                        // Read may return anything from 0 to numBytesToRead.
                        int n = fsSource.Read(bytes, 0, BufferLength);
                        // Break when the end of the file is reached.
                        if (n == 0)
                            break;
                        fsNew.Write(bytes, 0, n);
                        fsSource.Seek(n, SeekOrigin.Current);
                        fsNew.Seek(n, SeekOrigin.Current);
                        offset += n;
                        //percent = offset * 100 / fsSource.Length;
                    }
                    fsSource.Flush();
                    fsNew.Flush();
                    fsSource.Close();
                    fsNew.Close();
                    fsSource.Dispose();
                    fsNew.Dispose();
                }
            }
            catch (FileNotFoundException ioEx)
            {
                Console.WriteLine(ioEx.Message);
                throw ioEx;
            }
            return 0;
        }

        public static void DeleteFileMain()
        {
            MyFile df = new MyFile();
            df.GetAllDirList(@"C:\awork\Development\Pacom");
        }
    }
}
